package com.xiao.hao.service;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiao.hao.convertor.XhUserFileConvertor;
import com.xiao.hao.entity.XhUserFile;
import com.xiao.hao.facade.XhUserFileFacade;
import com.xiao.hao.facade.XhUserInfoFacade;
import com.xiao.hao.mapper.XhUserFileMapper;
import com.xiao.hao.req.XhUserFileInsertReq;
import com.xiao.hao.req.XhUserFileQueryPageReq;
import com.xiao.hao.req.XhUserFileUpdateReq;
import com.xiao.hao.utils.BaseId;
import com.xiao.hao.utils.PageResult;
import com.xiao.hao.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;


/**
 * @Description 用户附件表 服务实现层
 * @Author xiaohao
 * @Date 2025-03-05 17:57:41
 */
@Service
@Slf4j
public class XhUserFileServiceImpl implements XhUserFileFacade {

    @Resource
    XhUserFileMapper mapper;


    @Resource
    XhUserInfoFacade xhUserInfoFacade;

    /**
     * 功能描述: 角色新增
     *
     * @param req
     * @return com.xiao.hao.utils.Result
     * @author xiaohao
     * @date 2025-03-05 17:57:41
     */
    @Override
    public Result insert(XhUserFileInsertReq req) {
        return Result.succeed(mapper.insert(XhUserFileConvertor.INSTANCE.reqToEntity(req)));
    }


    /**
     * 功能描述: 角色修改
     *
     * @param req
     * @return com.xiao.hao.utils.Result
     * @author xiaohao
     * @date 2025-03-05 17:57:41
     */
    @Override
    public Result update(XhUserFileUpdateReq req) {
        return Result.succeed(mapper.updateById(XhUserFileConvertor.INSTANCE.reqToEntity(req)));
    }


    /**
     * 功能描述: 删除角色
     *
     * @param req
     * @return com.xiao.hao.utils.Result
     * @author xiaohao
     * @date 2025-03-05 17:57:41
     */
    @Override
    public Result delete(BaseId req) {
        return Result.succeed(mapper.deleteById(req.getId()));
    }

    /**
     * 功能描述: 角色分页查询
     *
     * @param req
     * @return com.xiao.hao.utils.PageResult
     * @author xiaohao
     * @date 2025-03-05 17:57:41
     */
    @Override
    public PageResult queryPage(XhUserFileQueryPageReq req) {
        Page<XhUserFile> page = mapper.selectPage(new Page(req.getNum(), req.getSize()), new LambdaQueryWrapper<XhUserFile>()
//                .eq(XhUserFile::getUserInfoId, UserConfig.getUserId())
                .orderByDesc(XhUserFile::getCreateTime));
        return PageResult.result(page.getTotal(), XhUserFileConvertor.INSTANCE.entityListToRespList(page.getRecords()));
    }

    @Override
    public Result upload(MultipartFile file) {
//        XhUserFile build = XhUserFile.builder().fileName(file.getOriginalFilename()).fileUrl(xhUserInfoFacade.upload(file)).build();
//        mapper.insert(build);
        return Result.succeed();
    }

}



